import request from '@/utils/request'
import { PageData } from '@/types/resp.ts'
import { BlogFile } from '@/types'
import {
  User,
  UserRole,
  LoginParam,
  ResetPwdParam,
  ChangePwdParam,
  UserPageParam,
  UserSaveOrUpdateParam,
  UserRemoveParam,
  UserRolesParam,
  UserRolesUpdateParam,
  UserUpdateParam,
  GitUpdateParam,
  MenuParam,
} from '@/types'

/*登陆接口*/
export const login = (param: LoginParam): Promise<User> => {
  return request({
    url: '/user/login',
    method: 'post',
    data: param,
  })
}

/*重置密码接口*/
export const passwordReset = (param: ResetPwdParam): Promise<User> => {
  return request({
    url: '/user/password/reset',
    method: 'post',
    data: param,
  })
}

/*修改密码接口*/
export const passwordChange = (param: ChangePwdParam): Promise<User> => {
  return request({
    url: '/user/password/change',
    method: 'post',
    data: param,
  })
}

/*分页查询接口*/
export const page = (param: UserPageParam): Promise<PageData<User>> => {
  return request({
    url: '/user/manage/page',
    method: 'post',
    data: param,
  })
}

/*新增修改接口*/
export const saveOrUpdate = (param: UserSaveOrUpdateParam): Promise<PageData<User>> => {
  return request({
    url: '/user/manage/saveOrUpdate',
    method: 'post',
    data: param,
  })
}

/*删除接口*/
export const remove = (param: UserRemoveParam): Promise<unknown> => {
  return request({
    url: '/user/manage/remove',
    method: 'post',
    data: param,
  })
}

export const roles = (param: UserRolesParam): Promise<UserRole> => {
  return request({
    url: '/user/manage/roles',
    method: 'post',
    data: param,
  })
}

export const rolesUpdate = (param: UserRolesUpdateParam) => {
  return request({
    url: '/user/manage/roles/update',
    method: 'post',
    data: param,
  })
}

export const avatarUpload = (file: File): Promise<BlogFile> => {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/user/avatar/update',
    method: 'post',
    data: formData,
  })
}

export const update = (param: UserUpdateParam): Promise<unknown> => {
  return request({
    url: '/user/update',
    method: 'post',
    data: param,
  })
}

export const gitUpdate = (param: GitUpdateParam) => {
  return request({
    url: '/user/git/update',
    method: 'post',
    data: param,
  })
}

export const menu = (param: MenuParam) => {
  return request({
    url: '/user/menu',
    method: 'post',
    data: param,
  })
}

export const check = () => {
  return request({
    url: '/user/check',
    method: 'post',
    data: {},
  })
}
